<?php
//$page['vars']['actions'] = [id_planet:int => [indexStructures:string => [[id: int, 'action': down|build, 'level': int, ('error': string)], …], …], …]
$indexStructures = $page['vars']['indexStructures'];
$actions = isset($page['vars']['actions']) ? $page['vars']['actions'] : array();

$completedLists = $player->getCompletedLists();
$currentIdPlanet = $planets->getCurrentId();

//$completedLists va contenir tout les élements fini
/*foreach ($planets->getIds() as $id_p)
{
	foreach ($page['vars']['allIndexStructures'] as $indexStruct)
	{
		if ($structure = $planets->getInstance($indexStruct, $id_p))
		{
			foreach ($structure->getCompletedLists()
				as $completedLists[$id_p][$indexStruct][]);
		}
	}
}*/


echo '<structures>';
foreach ($planets as $id_p => $planet)
{
	///@var array : [indexStruct:string => [id_element, …]], array : [string, …]
	$elementsStruct = $informations = array();

	//recupère les id des éléments (pour les afficher) et crée les balises d'informations
	if (isset($completedLists[$id_p]))
	{
		foreach ($completedLists[$id_p] as $indexStruct => $lists)
		{
			$informations[$indexStruct] = '';

			foreach ($lists as $list)
			{
				$elementsStruct[$indexStruct][] = $list;
				$informations[$indexStruct] .= '<element id="'.$list->getIdElement().'" level="'.$list->getLevel().'" action="finish"/>';
			}
		}
	}

	if ($currentIdPlanet === $id_p)
	{
		//ajoute des balises d'informations
		if (isset($actions[$id_p]))
		{
			foreach ($actions[$id_p] as $indexStruct => $id_elements_actions)
			{
				if (!isset($informations[$indexStruct]))
					$informations[$indexStruct] = '';

				$upgrade = ($indexStruct === U1_Research::INDEX) ? 'hour' : 'level';

				foreach ($id_elements_actions as $action)
				{
					$informations[$indexStruct] .= '<element id="'.$action['id'].'" '.$upgrade.'="'.$action['level'].'" action="'.$action['action'].'"'.(isset($action['error']) ? ' error="true">'.$action['error'].'</element>' : '/>');
				}
			}
		}

		echo '<structures id-planet="'.$id_p.'">';

		foreach ($indexStructures as $indexStruct)
		{
			echo '<structure type="'.$indexStruct.'">';
			if (isset($informations[$indexStruct]))
				echo '<informations>'.$informations[$indexStruct].'</informations>';

			$structure = $planets->getInstance($indexStruct, $id_p);

			echo '<waiting-list>';
			foreach($structure->getLists() as $l)
				echo '<element id="'.$l->getId()
					.'" level="'.$l->getLevel()
					.'" ref="'.$l->getIdElement()
					.'" duration="'.U1_Render::interval($l->getTimeEnd() - $_SERVER['REQUEST_TIME'])
				.'"/>';

			echo '</waiting-list>
			<elements can-build="'.((true === $structure->structureCanBuild($_SERVER['REQUEST_TIME'])) ? 'yes' : 'no').'">';

			$isResearch = ($structure instanceof U1_Research);

			foreach ($structure->getGroupsUnblock() as $group => $elements)
			{
				echo '<elements group="'.$group.'">';
				foreach ($elements as $id_e => $element)
				{
					echo '<element id="'.$id_e.'" level="'.$element->getLevel().($isResearch ? '" percentage="'.$element->getPercent().'"><cost>' : '"><cost>');

					foreach ($element->getPrice() as $id => $cost)
						echo '<resource group="'.$id.'">'.$cost.'</resource>';

					echo '</cost>';

					if ($isResearch)
						echo '<percentage>'.$element->getGainForTheDuration().'</percentage>';

					echo '<duration>'.U1_Render::interval($element->getDuration()).'</duration></element>';
				}
				echo '</elements>';
			}
			echo '</elements>
			</structure>';
		}
		echo '</structures>';

		U1_Render::addResource($id_p);
	}
	else if ($elementsStruct)
	{
		echo '<structures id-planet="'.$id_p.'">';
		foreach ($elementsStruct as $indexStruct => $lists)
		{
			$elementsStruct = array();
			$structure = $planets->getInstance($indexStruct, $id_p);

			foreach ($lists as $list)
			{
				$id_e = $list->getIdElement();
				$elementsStruct[$structure->get($id_e)->getGroup()][$id_e] = $list;
			}

			echo '<structure type="'.$indexStruct.'">';
			if ($informations[$indexStruct])
				echo '<informations>'.$informations[$indexStruct].'</informations>';

			echo '<elements>';
			foreach ($elementsStruct as $group => $lists)
			{
				echo '<elements group="'.$group.'">';
				foreach ($lists as $id_e => $list)
					echo '<element id="'.$id_e.'" level="'.$list->getLevel().'"/>';
				echo '</elements>';
			}
			echo '</elements>
			</structure>';
		}
		echo '</structures>';
	}
}
echo '</structures>';